#ifndef MYWINUSB_H
#define MYWINUSB_H

#include <QObject>
#include <QString>
#include <QMessageBox>



#include "windows.h"
#include "winusb.h"
#include "tchar.h"
#include "strsafe.h"
#include "string.h"
#include <sys/time.h>
#include <sys/time.h>

#include "setupapi.h"
#include "usbuser.h"
#include "winusbio.h"
#include "usbspec.h "
#include "initguid.h"
#include "guiddef.h"

#include <dbt.h>



class mywinusb : public QObject {
    Q_OBJECT

public:
    explicit mywinusb(QObject *parent = nullptr);
    WINUSB_SETUP_PACKET SetupPacket;
    struct PIPE_ID
    {
        UCHAR  PipeInId[5];
        UCHAR  PipeOutId[5];
    };

    PIPE_ID PipeID = {0,0,0,0,0,0,0,0,0,0};//定义结构体

    // 全局变量
    //DEFINE_GUID (GUID_DEVINTERFACE_WINUSB, 0xdee824ef, 0x729b, 0x4a0e, 0x9C, 0x14, 0xb7, 0x11, 0x7d, 0x33, 0xa8, 0x17);

    BOOL setUserUSBDeviceGUID(QString guid);
    BOOL setUserUSBDeviceVID_PID(uint16_t vid, uint16_t pid);

    BOOL setUSBDeviceConnect(void);
    BOOL getUSBConnectState(void);

    void usbErrorHandler(void);

    BOOL  GetDeviceHandleBy_GUID(CONST GUID *InterfaceClassGuid);
    BOOL  GetDeviceHandleBy_VID_PID(uint16_t vendorId, uint16_t productId);


    BOOL  GetWinUSBHandle(void);
    BOOL  GetUSBDeviceSpeed(void);
    BOOL  QueryDeviceEndpoints(void);
    BOOL  WriteToBulkEndpoint( UCHAR* pID, UCHAR *sendtext, ULONG wLen);
    int   ReadFromBulkEndpoint(UCHAR* pID, UCHAR* readBuffer, ULONG maxLen);

    BOOL SendDatatoDefaultEndpoint(void);
    BOOL ReadDatatoDefaultEndpoint(void);
    BOOL GetUserInterfaceConfig(void);

signals:
     void printErrorInfo(QString s);

private:
    HANDLE hDeviceHandle = INVALID_HANDLE_VALUE;
    WINUSB_INTERFACE_HANDLE hWinUSBHandle = INVALID_HANDLE_VALUE;
    BOOL connectResult = false;
    UCHAR DeviceSpeed = 0;//定义设备的速度
    GUID guidDeviceInterface;
    uint16_t userPID=0, userVID=0;

    void usbErrorPrint(QString func,int line);
    void usbErrorPrint(QString errinfo);

};

#endif // MYWINUSB_H
